<template>
  <div>
    <div class="top"><strong>首页</strong> > 商品管理 > 添加商品</div>
    <el-card>
      <el-alert title="添加商品信息" type="info" center show-icon> </el-alert>
      <el-steps
        align-center
        :space="300"
        :active="acti - 0"
        finish-status="success"
        class="step"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="addFromRef"
        label-width="100px"
        class="demo-ruleForm"
        label-position="top"
       
      >
        <el-tabs tab-position="left" class="tabs" v-model="acti"  :before-leave='beforelsess' @tab-click='gost'>
          <el-tab-pane label="基本信息" name="0" >
            <el-form-item label="商品名称 " prop="goods_name">
              <el-input v-model="ruleForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格 " prop="goods_price">
              <el-input v-model="ruleForm.goods_price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="商品重量 " prop="goods_weight">
              <el-input
                v-model="ruleForm.goods_weight"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量 " prop="goods_number">
              <el-input
                v-model="ruleForm.goods_number"
                type="number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品分类 " prop="goods_s">
              <el-cascader
                v-model="ruleForm.goods_cat"
                :options="catelist"
                :props="sol"
                style="width: 30%"
                clearable
                class="cass"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
              <el-form-item :label='m.attr_name' v-for="m in sgo" :key='m.attr_id'>
                  <el-checkbox-group v-model="m.attr_vals">
                <el-checkbox :label="mi" v-for="(mi,i) in m.attr_vals" :key="i" border></el-checkbox>
               </el-checkbox-group>
              </el-form-item>
              
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
              <el-form-item :label='m.attr_name' v-for="m in onlylsit" :key='m.attr_id'>
                 <el-input v-model="m.attr_vals"></el-input>
              </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
            
                action="http://127.0.0.1:8888/api/private/v1/upload"
                :on-remove='handremove'
                list-type="picture"
                :headers='scoc'
                :on-success='handsuccess'
                :on-preview='handpreview'
                >
                <el-button size="small" type="primary">点击上传</el-button>
               
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quill-editor v-model="ruleForm.goods_introduce"></quill-editor>
                       <el-button type="primary" @click="hot" class="jok"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <el-dialog
  title="图片预览"
  :visible.sync="dialogVisible"
  width="50%"
  >
  <img :src="prive" alt="" class="jkl">
</el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getcatelist()
  },
  data() {
    return {
        dialogVisible:false,
        prive:'',
        //图片请求头
        //handpic:[],
        scoc:{ 
            Authorization:window.sessionStorage.getItem('token')
        },
        onlylsit:[],
      sol: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
      },
      gosy:[],
      sgo:[],
      catelist: [],
      gosy: [],
      acti: '0',
      ruleForm: {
        goods_name: '',
        goods_price: '0',
        goods_number: '0',
        goods_weight: '0',
        //保存图片的数组
        pics:[],
       // 保存商品分类数组
        goods_cat: [],
        //商品的详细描述
        goods_introduce:'',
        attrs:[]
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur',
          },
        ],
        goods_number: [
          {
            required: true,
            message: '请输入商品数量',
            trigger: 'blur',
          },
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur',
          },
        ],
        goods_s: [{}],
      },
    }
  },
  methods: {
    async getcatelist() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.catelist = res.data
    },
   beforelsess(activename,oldname){
    //    this.$refs.addFromRef.validate( (valid) => {
       
    //     if (!valid) return false
    //    })
       if(oldname==='0'&&this.ruleForm.goods_cat.length!==3){
           this.$message.error('请选择商品分类')
           return false
       }
    },
   async gost(){
        if(this.acti==1){
            const{data:res} =await this.$http.get(`categories/${this.ruleForm.goods_cat[2]}/attributes`,{params:{sel:'many'}
            })        
            if(res.meta.status!==200){
                this.$message.error(res.meta.msg)
            }
            //console.log(res);
            res.data.forEach(item=>{
                item.attr_vals=item.attr_vals.length==0?[]:item.attr_vals.split(' ')
            })
            this.sgo=res.data
            console.log(this.sgo);
            }
        if(this.acti==2){
             const{data:res} =await this.$http.get(`categories/${this.ruleForm.goods_cat[2]}/attributes`,{params:{sel:'only'}
            }) 
            if(res.meta.status!==200){
                this.$message.error(res.meta.msg)
            }
             this.onlylsit = res.data
        }
           
    },
  handsuccess(respone){
     const picn = {
         pic:respone.data.tmp_path
     }
     this.ruleForm.pics.push(picn)
      //console.log(this.handpic);
  },
  handremove(file){
      console.log(file);
      const filepos = file.response.data.tmp_path
     const i = this.ruleForm.pics.findIndex(x => x.pic==filepos)
  this.ruleForm.pics.splice(i,1)
      //console.log(this.handpic);
  },
  handpreview(file){
    this.prive=file.response.data.url
    this.dialogVisible=true
    //console.log(this.prive);
  },
  hot(){
       this.$refs.addFromRef.validate(async valid=>{
           if(!valid) return this.$message.error('请填写必要的参数和信息')
           //this.ruleForm.goods_cat=this.gosy
           //console.log(ruleForm.goods_cat);
         //this.ruleForm.goods_cat=  this.ruleForm.goods_cat.jion(',')
         const form = JSON.parse(JSON.stringify(this.ruleForm))
            form.goods_cat = form.goods_cat.join(',')
           this.onlylsit.forEach(item=>{
               const info={
                   attr_id:item.attr_id,
                   attr_value:item.attr_value
               }
            form.attrs.push(info)
           })
           this.sgo.forEach(item=>{
               const newinfo={
                   attr_id:item.attr_id,
                   attr_value:item.attr_value
               }
            form.attrs.push(newinfo)
           })
           
            const{data:res}=await this.$http.post('goods',
                form
            )
            if(res.meta.status!==201){
              return this.$message.error(res.meta.msg)
            }
            this.$message.success(res.meta.msg)
            this.$router.push('/goods')
       })
  }
}
}
</script>

<style lang='less' scope>
.top {
  margin-left: 20px;
  margin-top: 25px;
  font-size: 14px;
  margin-bottom: 20px;
}
.step {
  margin-top: 20px;
}
.tabs {
  margin: 20px;
}
.el-checkbox{
    margin: 0 5px 0 0 !important;
}
.jkl{
    width: 100%;
    height: 600px;
}
.ql-editor{
    height: 300px !important;
}
 .jok{
     margin-top:10px !important;
 }
</style>